package com.proyecto.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.proyecto.modelo.Vehiculo;
import com.proyecto.modelo.Usuario;
import com.proyecto.servicio.UsuarioSrv;
import com.proyecto.servicio.VehiculoSrv;

@Controller
@SessionAttributes("login_user")
@RequestMapping("/vehiculo/")
public class VehiculoControler {
	
	@Autowired
	private VehiculoSrv vehiculoSrv;
	
	@Autowired(required=false)
	Validator validator;
	
	@Autowired
	private UsuarioSrv usuarioSrv;
	
	@RequestMapping("/listaVehiculos.do")
	public ModelAndView listaVehiculo (HttpServletRequest request, 
			HttpServletResponse responce, 
			@ModelAttribute("vehiculo") Vehiculo vehiculo ){
		ModelAndView modelAndView = new ModelAndView ("/vehiculo/listaVehiculos");
		modelAndView.addObject("vehiculos", vehiculoSrv.obtenerVehiculos());
		modelAndView.addObject("vehiculo", vehiculo);
//		modelAndView.addObject("vehiculosList", vehiculoSrv.obtenerVehiculos());
		return modelAndView;
	}
	
	@RequestMapping("/editarVehiculo.do")
	public ModelAndView vehiculo (HttpServletRequest request, 
			HttpServletResponse responce, 
			@ModelAttribute("vehiculo") Vehiculo vehiculo ){
		ModelAndView modelAndView = new ModelAndView ("/vehiculo/vehiculoForm");
		Integer patente = (Integer) request.getAttribute("patente");
		modelAndView.addObject("vehiculo", vehiculoSrv.obtenerVehiculo(patente));
		return modelAndView;
	}
	
}
